package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.JWTUtil;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TCar;
import com.woniu.car.mapper.TCarMapper;
import com.woniu.car.parameter.AddCarParam;
import com.woniu.car.service.TCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author zhoubinbin
 * @since 2021-06-21
 */
@Service
public class TCarServiceImpl extends ServiceImpl<TCarMapper, TCar> implements TCarService {
    @Resource
    private TCarMapper carMapper;
    //新增车辆信息
    @Override
    public ResponseEntity addCar(AddCarParam addCarParam, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            return new ResponseEntity(500,"您当前未登陆,请登陆后进行操作",null);
        }
        String id = JWTUtil.getId(token);
        Integer userId = Integer.valueOf(id);
        TCar tCar = new TCar();
        tCar.setUserId(userId);
        BeanUtils.copyProperties(addCarParam,tCar);
        int insert = carMapper.insert(tCar);
        if (insert>0){
            return new ResponseEntity(200,"新增成功",addCarParam);
        }else{
            return new ResponseEntity(400,"新增失败",null);
        }
    }

    @Override
    public ResponseEntity selectCar(Integer userId) {
        QueryWrapper<TCar> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<TCar> tCars = carMapper.selectList(wrapper);
        if (tCars.size()==0){
            return new ResponseEntity(200,"用户未添加车辆信息",null);
        }else {
            return new ResponseEntity(200,"查询成功",tCars);
        }
    }
}
